<?php
    
    session_start();
    /**
        * Site
        * Author: Bruno Ramos <contato [AT] brunoramos [DOT] eti [DOT] br>
        * File: index.php
        * Version: 1.0
        */

    // define diretorio do Smarty
    define('SMARTY_DIR', '../libs/');
    require(SMARTY_DIR . 'Smarty.class.php');

    // Inicializa Smarty e configuracoes
    include('libs/smarty.init.php');

    // inclui arquivo com classes
    include('libs/classes.php');

    // Url Amigavel
    require_once('libs/geturl.php');
    
    // Seta ou modificar idioma
    require_once ("libs/lang.php");

    // PHP IDS - Protecao contra url injection
    require_once 'IDS/Init.php';
    $request = array('REQUEST' => $_REQUEST,'GET' => $_GET,'POST' => $_POST,'COOKIE' => $_COOKIE);
    $init = IDS_Init::init('IDS/Config/Config.ini.php');
    $ids = new IDS_Monitor($request, $init);
    $result = $ids->run();
    if (!$result->isEmpty()) {
        // Enviar resultado para administrador
        $corpo = "Caro ".$config["webmaster_nome"].", <br> Houve uma tentativa de URL Injection em: ".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]."<br>";
        $corpo .= "Do IP: ".$_SERVER['REMOTE_ADDR']."<br>";
        $corpo .= "Relatorio: <br><br>".$result;
        //mail($config["webmaster_email"], "Tentativa de URL Injection", $corpo);
    }

    // Limpa cache caso faca login
    if (isset($_SESSION["cacheClear"])) { 
            $smarty->clearCache('index.html');
            unset($_SESSION["cacheClear"]);
    }

    // Limpa cache das paginas definidas no config
    foreach($nocache_url as $tmp_url)
            $smarty->clearCache($tmp_url);

    
    // Manipula url
    if($url[0] == "logout" || $_POST["logout"] == 1) {
            session_destroy();
            $smarty->clearCache('index.html');
            if(!$_POST["logout"])
                    header("Location: ".$baseUrl);
    } else if(substr($url[0],0,8) == "login_fb" || substr($url[0],0,8) == "login_tw") {
            $smarty->clearCache('index.html');
            include("libs/tpl_".substr($url[0],0,8).".html.php");
    }
    else if(!empty($url[0]) && !is_file('templates/'.$config['template'].'/'.$url[0].'.html') && $url[0] != "lang")
            require_once("404.php");
    else {
            if($url[0] == "lang")
                $url[0] = "index";
            
            $show = (!empty($url[0]))? $url[0].".html" : "index.html";
            if(is_file("libs/tpl_".$show.".php"))
                    include("libs/tpl_".$show.".php");
            //lida com cache para busca
            $cacheDif = (empty($url[1]))? $_POST["pesquisa"] : $url[1];
            $smarty->display($show, $cacheDif.$_SESSION["langId"]);
    }
?>
